#!/usr/bin/env python
#
# Copyright 2007 Google Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

import cgi

from google.appengine.ext import db
from google.appengine.ext import webapp
from google.appengine.ext.webapp.util import run_wsgi_app

class Review(db.Model):
  author = db.StringProperty()
  authorOrkutId = db.StringProperty()
  owner = db.StringProperty()
  ownerOrkutId = db.StringProperty()
  inteligence = db.IntegerProperty()
  humor = db.IntegerProperty()
  kiss = db.IntegerProperty()
  cat = db.IntegerProperty()
  faith = db.IntegerProperty()
  beauty = db.IntegerProperty()
  body = db.IntegerProperty()
  comment = db.StringProperty(multiline=True)
  date = db.DateTimeProperty(auto_now_add=True)

class Complaint(db.Model):
  author = db.StringProperty()
  requester = db.StringProperty()
  reasons = db.StringProperty(multiline=True)
  date = db.DateTimeProperty(auto_now_add=True)

class Complain(webapp.RequestHandler):
  def post(self):
    query = db.GqlQuery("SELECT * FROM Complaint WHERE author = :author "
	                      "AND requester = :requester ", author = self.request.get('author'),
						  requester = self.request.get('requester'))
    result = query.fetch(1)
    db.delete(result)

    complain = Complaint()
    complain.author = self.request.get('author')
    complain.requester = self.request.get('requester')
    complain.reasons = self.request.get('reasons')
    complain.put()
    self.response.out.write('{ \"Error\" : \"200\", \"Result\" : \"OK\"}')

class Fetch(webapp.RequestHandler):
  def post(self):
    reviews = db.GqlQuery("SELECT * FROM Review WHERE owner = :owner "
							"ORDER BY date DESC", owner = self.request.get('owner'))
    num = reviews.count()
    i = 1
	
    self.response.out.write('{"Error" : "200", "Result" : "OK", "Reviews" : [')
    for review in reviews:
      if i == num:
        self.response.out.write('{"Author" : "%s", "Owner" : "%s", "Inteligence" : "%d", "Humor" : "%d", "Kiss" : "%d", "Cat" : "%d", "Faith" : "%d", "Beauty" : "%d", "Body" : "%d", "Comment" : "%s"}' %(review.author, review.owner, review.inteligence, review.humor, review.kiss, review.cat, review.faith, review.beauty, review.body, review.comment))
      else:  
        self.response.out.write('{"Author" : "%s", "Owner" : "%s", "Inteligence" : "%d", "Humor" : "%d", "Kiss" : "%d", "Cat" : "%d", "Faith" : "%d", "Beauty" : "%d", "Body" : "%d", "Comment" : "%s"},' %(review.author, review.owner, review.inteligence, review.humor, review.kiss, review.cat, review.faith, review.beauty, review.body, review.comment))
        i = i+1
    self.response.out.write(']}')

class Deploy(webapp.RequestHandler):
  def post(self):
    query = db.GqlQuery("SELECT * FROM Review WHERE author = :author "
	                      "AND owner = :owner ", author = self.request.get('author'),
						  owner = self.request.get('owner'))
    result = query.fetch(1)
    db.delete(result)
    
    review = Review()
    review.author = self.request.get('author')
    review.authorOrkutId = self.request.get('authorOrkutId')
    review.owner = self.request.get('owner')
    review.ownerOrkutId = self.request.get('ownerOrkutId')
    review.inteligence = int(self.request.get('inteligence'))
    review.humor = int(self.request.get('humor'))
    review.kiss = int(self.request.get('kiss'))
    review.cat = int(self.request.get('cat'))
    review.faith = int(self.request.get('faith'))
    review.beauty = int(self.request.get('beauty'))
    review.body = int(self.request.get('body'))
    review.comment = self.request.get('comment')
    if int(self.request.get('inteligence')) < 0 or int(self.request.get('inteligence')) > 5 or int(self.request.get('humor')) < 0 or int(self.request.get('humor')) > 5 or int(self.request.get('kiss')) < 0 or int(self.request.get('kiss')) > 5 or int(self.request.get('cat')) < 0 or int(self.request.get('cat')) > 5 or int(self.request.get('faith')) < 0 or int(self.request.get('faith')) >5 or int(self.request.get('beauty')) < 0 or int(self.request.get('beauty')) > 5 or int(self.request.get('body')) < 0 or int(self.request.get('body')) > 5:
      self.response.out.write('{ \"Error\" : \"500\", \"Result\" : \"ERROR\"}')
    else:
      review.put()
      self.response.out.write('{ \"Error\" : \"200\", \"Result\" : \"OK\"}')

class Delete(webapp.RequestHandler):
  def post(self):
    query = db.GqlQuery("SELECT * FROM Review WHERE author = :author "
	                      "AND owner = :owner ", author = self.request.get('author'),
						  owner = self.request.get('owner'))
    result = query.fetch(1)
    db.delete(result)
    self.response.out.write('{ \"Error\" : \"200\", \"Result\" : \"OK\"}')

application = webapp.WSGIApplication(
                                     [('/deploy', Deploy),
									 ('/fetch', Fetch),
									 ('/delete', Delete),
									 ('/complain', Complain)],
                                     debug=True)

def main():
  run_wsgi_app(application)

if __name__ == "__main__":
  main()